package com.peko.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：Peko
 * @Description：TODO
 * @Date：Created in 16:37 2023/8/1
 */

@RestController
public class DemoController {

    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @GetMapping("demo")
    //熔断之后的快速响应（这是服务方的熔断，也可以在消费方做熔断，例如在openFeign层面上做熔断）
    //优先自定义的失败响应
    @HystrixCommand(fallbackMethod = "demoFailBack",defaultFallback = "defaultFallback")
    public String demo(Integer id){
        log.info("id = {}",id);
        if(id <= 0 ){
            throw new RuntimeException("非正数！");
        }
        return "demo ok!";
    }

    //默认的处理方法，要求返回值必须是String，无参
    public String  defaultFallback(){
        return "默认的失败处理响应";
    }

    //响应方法必须与原方法的返回值和参数一致！
    public String demoFailBack(Integer id){
        return "熔断！";
    }
}
